package com.liyi.dao;

import java.util.Date;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;

import com.zero.basic.dao.DefaultBaseDao;
import com.zero.basic.dao.SqlQueryBaseDao;
import com.zero.basic.exception.SmsDaoException;
import com.zero.basic.util.SimpleDateUtil;

@Repository("formulaDaoLiyi")
public class FormulaDaoImpl extends DefaultBaseDao<FormulaDao> implements FormulaDao {

	@Inject
	private SqlQueryBaseDao<?> sqlQueryBaseDao;
	
	@Inject
	private EmpSalaryDao empSalaryDaoLiyi;
	
	// 获取某 [员工] 在某 [月] 的 [计算工资项] 中对应公式的左端运算子
	@Override
	public Integer getLeftOperand(Integer empId, String date, Integer bindCalItemId) throws SmsDaoException {
		try {
			// 如果未指定公式生效月，则默认本月
			if (date == null) {
				date = SimpleDateUtil.generateDateString(new Date());
			}
			// 声明SQL语句
			String sql = "select c.cal_item from t_cal_item c where c.item_id = " + bindCalItemId + " and c.cal_date = (select MIN(c.cal_date) from t_cal_item c where c.cal_date >= '" + date + "')";
			// 获取查询结果：公式左端运算子(导入工资项的id)
			Integer importItemId = (Integer) sqlQueryBaseDao.queryBySql(sql, null, null, false, false);
			// 二次查询结果：公式左端运算子(导入工资项的值)
			Integer leftOperand = empSalaryDaoLiyi.getSalaryNum(empId, date, importItemId);
			// 返回查询结果
			return leftOperand;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}
	
	// 获取某 [员工] 在某 [月] 的 [计算工资项] 中对应公式的运算符
	@Override
	public String getOperator(Integer empId, String date, Integer bindCalItemId) throws SmsDaoException {
		try {
			// 如果未指定公式生效月，则默认本月
			if (date == null) {
				date = SimpleDateUtil.generateDateString(new Date());
			}
			// 声明SQL语句
			String sql = "select c.cal_operator from t_cal_item c where c.item_id = " + bindCalItemId + " and c.cal_date = (select MIN(c.cal_date) from t_cal_item c where c.cal_date >= '" + date + "')";
			// 获取查询结果
			String operator = (String) sqlQueryBaseDao.queryBySql(sql, null, null, false, false);
			// 返回查询结果
			return operator;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}
	
	// 获取某 [员工] 在某 [月] 的 [计算工资项] 中对应公式的右端运算子
	@Override
	public Double getRightOperand(Integer empId, String date, Integer bindCalItemId) throws SmsDaoException {
		try {
			// 如果未指定公式生效月，则默认本月
			if (date == null) {
				date = SimpleDateUtil.generateDateString(new Date());
			}
			// 声明SQL语句
			String sql = "select c.cal_seNum from t_cal_item c where c.item_id = " + bindCalItemId + " and c.cal_date = (select MIN(c.cal_date) from t_cal_item c where c.cal_date >= '" + date + "')";
			// 获取查询结果
			Double rightOperand = (Double) sqlQueryBaseDao.queryBySql(sql, null, null, false, false);
			// 返回查询结果
			return rightOperand;
		} catch (Exception e) {
			throw new SmsDaoException();
		}
	}
}